﻿using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using KZ.Exception;
using KZ.Lottery.Entities;
using KZ.Lottery.Resources;
using KZ.Utilities;

namespace KZ.Lottery.DAL
{
    public class SettingDAL
    {
        private static Setting CreateSettingFromReader(SqlDataReader reader)
        {
            Setting setting = new Setting
            {
                SettingID = Utility.StringParse(reader[SettingColumns.SettingID.ToString()]),
                SettingValue = Utility.StringParse(reader[SettingColumns.SettingValue.ToString()]),
            };

            return setting;
        }

        public static List<Setting> GetSettingList(string settingID)
        {
            try
            {
                List<Setting> settingList = new List<Setting>();

                DataAccessHelper.SqlCmd.Parameters.Add(SettingColumns.SettingID.ToString(), SqlDbType.NVarChar).Value = settingID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.SettingGetList))
                {
                    while (reader.Read())
                    {
                        settingList.Add(CreateSettingFromReader(reader));
                    }
                }

                return settingList;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetSettingList", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTING_GET);
            }
        }

        public static Setting GetSetting(string settingID)
        {
            try
            {
                Setting setting = new Setting();

                DataAccessHelper.SqlCmd.Parameters.Add(SettingColumns.SettingID.ToString(), SqlDbType.NVarChar).Value = settingID;
                using (SqlDataReader reader = DataAccessHelper.ExecuteQuery(CommandType.StoredProcedure, KZ_StoreName.SettingGetByID))
                {
                    while (reader.Read())
                    {
                        setting = CreateSettingFromReader(reader);
                    }
                }

                return setting;
            }
            catch (SqlException ex)
            {
                Log.LogEx("GetSetting", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTING_GET);
            }
        }

        public static bool CheckConnection()
        {
            try
            {
                DataAccessHelper.RefeshConnection();

                DataAccessHelper.BeginTransaction(IsolationLevel.Serializable);
                DataAccessHelper.CommitTransaction();

                return true;
            }
            catch (System.Exception ex)
            {
                Log.LogEx("CheckConnection", ex);
                return false;
            }
        }

        public static bool UpdateSetting(List<Setting> settingList)
        {
            try
            {
                DataAccessHelper.BeginTransaction(IsolationLevel.Serializable);

                SettingDAL settingDAL = new SettingDAL();
                foreach (Setting item in settingList)
                {
                    settingDAL.UpdateSetting(item);
                }
                DataAccessHelper.CommitTransaction();

                return true;
            }
            catch (DataAccessException)
            {
                DataAccessHelper.RollbackTransaction();
                throw;
            }
        }

        public bool UpdateSetting(Setting setting)
        {
            try
            {
                DataAccessHelper.SqlCmd.Parameters.Add(SettingColumns.SettingID.ToString(), SqlDbType.VarChar).Value = setting.SettingID;
                DataAccessHelper.SqlCmd.Parameters.Add(SettingColumns.SettingValue.ToString(), SqlDbType.VarChar).Value = setting.SettingValue;

                if (DataAccessHelper.ExecuteNonQueryWithTransaction(CommandType.StoredProcedure, KZ_StoreName.SettingUpdate) > 0)
                {
                    return true;
                }
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTING_UPDATE);
            }
            catch (SqlException ex)
            {
                Log.LogEx("UpdateSetting", ex);
                throw new DataAccessException(KZ_Message.BLM_ERR_SETTING_UPDATE);
            }
        }
    }
}